const fs = require('fs');
const path = require('path');

//一键生成配置文件 依次递归组合所有选项 计算id和label值,请使用node生成json文件
let arr = [{ id: 2, label: "1-1.稳定脱贫户", value: "lowIncomeFamily" },
{ id: 16, label: "2-1.城镇低保", value: "orphan" },
{ id: 32, label: "2-2.农村低保", value: "martyrChildren" },
{ id: 64, label: "3-1.城镇特困", value: "disability" },
{ id: 128, label: "3-2.农村特困", value: "other" },
{ id: 256, label: "4-1.孤儿", value: "recordManagement" },
{ id: 512, label: "4-2.事实无人抚养儿童", value: "villageLowIncomeFamily" },
{ id: 1024, label: "5.烈士子女", value: "wuRenFuYang" },
{ id: 2048, label: "6.残疾学生", value: "nongCunTeKun" },
{ id: 4096, label: "7-3.其他困难学生", value: "BianYuanHu" },
{ id: 8192, label: "1-2.脱贫不稳定户" },
// { id: 8192, label: "1-2.脱贫不稳定户" },
{ id: 16384, label: "7-1.边缘易致贫户" },
{ id: 32768, label: "7-2.突发严重困难户" },
{ id: 65536, label: "7-4.涉农专业学生" },// 原 12 变 8192 ， 0912
];
function deal (arr, len = 1) {
  let res = []
  function deep (temp, start) {
    if (temp.length === len) {
      res.push({
        id: temp.reduce((p, c) => {
          p = p + c.id
          return p
        }, 0),
        label: temp.map(item => item.label).join("|")
      })
      return;
    }
    for (let index = start; index < arr.length; index++) {
      start++
      temp.push(arr[index]);

      deep(temp, start)
      temp.pop()
    }
  }
  deep([], 0);

  return res;
}


function while_deep (arr, start = 2, len = 4) {
  let res_deep = []
  // console.log(deal(arr, start))
  while (start <= len) {
    res_deep.push(...deal(arr, start))
    start++;
  }
  return res_deep
}

let res = while_deep(arr)

let filePath = path.join(__dirname, "./povertyStudent.json");
let writeFile = () => {
  fs.writeFile(filePath, JSON.stringify(res), "utf-8", (err) => {
    if (err) {
      console.log(err)
    }
  })
}
console.log(filePath)
fs.readdir(filePath, "utf-8", (err, files) => {
  if (err) {
    writeFile()
  } else {
    fs.unlink(filePath, (unlinkErr) => {
      if (unlinkErr) {
        console.error(`删除文件失败: ${unlinkErr.message}`);
      } else {
        writeFile()
        console.log(`${filePath}配置文件已生成`);
      }
    });

  }
});

